<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义对象</title>
</head>
<body>
    <table border="1">
        <caption>商品列表</caption>
        <tr>
            <th>标题</th>
            <th>价格</th>
            <th>销量</th>
        </tr>
    </table>
    <script>
        // 实例化一个空对象
        let p = {}
        p.name = "张三"
        p.age = 18
        p.run = function(){
            console.log(this.name+":"+this.age)
        }
        p.run()
        // 自带属性和方法的对象
        let p1={
            name : "李四",
            age:20,
            run:function(){
                console.log(this.name+":"+this.age)
            }
        }
        p1.run();
        // 通过对象封装一个商品信息
        let product = {title:"小米手机",price:3000,number:100}
        // 通过数组封装对象的形式,封装多个商品的信息
        let arr = [{title:"小米手机",price:3000,num:100},
        {title:"华为手机",price:3000,num:100},
        {title:"联想手机",price:3000,num:100}]
        // 得到页面中的表格
        let table = document.querySelector("table")
        // 遍历数组
        for ( let p of arr) {
            let tr =document.createElement("tr")
            let titleTd = document.createElement("td")
            let priceTd =document.createElement("td")
            let numTd= document.createElement("td")

            // 将数组中的数据添加的td
            titleTd.innerText=p.title;
            priceTd.innerText  =p.price;
            numTd.innerText=p.num;
            // 将td存入到tr
            tr.append(titleTd,priceTd,numTd)
            // 将tr存入table
            table.append(tr)
            // 将表格存入到页面
            document.body.append(table)
        }
    </script>
</body>
</html>